home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10127 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1009 b   |  36 lines

  1. Path: iz.maus.de!Torsten_Landschoff
  2. From: Torsten_Landschoff@iz.maus.de (Torsten Landschoff)
  3. Newsgroups: comp.lang.c++
  4. Subject: Can copy constructor and operator= share code?
  5. Message-ID: <199603040709.a35476@iz.maus.de>
  6. Date: Mon, 04 Mar 96 05:09:00 GMT
  7. References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM>
  8. X-Gate: MausGate/News 1.25/ac3
  9. MIME-Version: 1.0
  10. Content-Type: text/plain; charset=ISO-8859-1
  11. Content-Transfer-Encoding: 8bit
  12.  
  13. -A13545@AC3
  14.  
  15. Hi Boris
  16.  
  17. BB>I have tried calling the copy constructor from operator=, but nothing
  18. BB>happens.  I've gotten around this problem by creating a private Copy()
  19. BB>function, which is called by both the copy constructor and operator=.
  20. BB>Is there another way?
  21.  
  22. This is the wrong way round but you can call the operator = from the copy
  23. constructor: 
  24.  
  25. class A {
  26.     ...
  27.   public:
  28.     A &operator =(const A &);   // declare the assignment operator
  29.     A( const A &src ) {
  30.         *this = src;            // call the assignment operator
  31.     }
  32. };
  33.  
  34. Hope it helps
  35.     Torsten
  36.